package cn.com.bluemoon.daps.model.core.file.pgsql;

import cn.com.bluemoon.daps.common.validation.illegal.IllegalChar;
import cn.com.bluemoon.daps.model.core.file.FieldData;
import cn.com.bluemoon.daps.model.core.file.excel.converter.CustomBooleanStringConverter;
import cn.com.bluemoon.daps.model.entity.DapDataModelTableField;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import com.alibaba.excel.annotation.write.style.HeadRowHeight;
import lombok.Data;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import java.util.Optional;

/**
 * <pre>
 * 序号	表英文名	字段名	数据类型	长度	主键	非空	缺省	字段中文名	字段业务描述
 * 1	b2b_User_Huanxin_Info	id	bigint		yes	no		EC订单编号	内部订单编号
 * </pre>
 *
 * @author Jarod.Kong
 * @date 2021/2/5 17:23
 */
@Data
@ColumnWidth(20)
@HeadRowHeight(15)
@ContentRowHeight(20)
public class PgSqlFieldData extends FieldData {
    @ExcelProperty("序号")
    @ColumnWidth(8)
    private Integer order;

    //    @NoSqlKeyword(dbtype = DbType.postgresql, name = "表英文名")
    @IllegalChar(message = "表英文名存在非法字符")
    @ExcelProperty("表英文名")
    @Size(max = 64, message = "表英文名不可以大于{max}")
    @NotEmpty(message = "表英文名不可为空")
    private String tableName;

    //    @NoSqlKeyword(dbtype = DbType.postgresql, name = "字段名")
    @IllegalChar(message = "字段名存在非法字符")
    @ExcelProperty("字段名")
    @Size(max = 64, message = "字段名称不可以大于{max}")
    @NotEmpty(message = "字段名不可为空")
    private String fieldName;

    @ExcelProperty("数据类型")
    @Size(max = 64, message = "数据类型不可以大于{max}")
    @NotEmpty(message = "数据类型不可为空")
    private String filedDataType;

    @ExcelProperty("长度")
    @Size(max = 1024, message = "数据长度不可以大于{max}")
    private String fieldLength;

    @ExcelProperty("小数位")
    @Pattern(regexp = "^\\d+$", message = "小数位必须为非负整数")
    private String decimalLength;

    @ExcelProperty(converter = CustomBooleanStringConverter.class, value = "主键")
    private Boolean primaryKey;

    @ExcelProperty(converter = CustomBooleanStringConverter.class, value = "非空")
    private Boolean notNull;

    @ExcelProperty("缺省")
    private String defaultVal;

    @ExcelProperty("字段中文名")
    @Size(max = 1024, message = "字段中文名不可以大于{max}")
    @NotEmpty(message = "字段中文名不可为空")
    private String fieldNameZh;

    @ExcelProperty("字段业务描述")
    @Size(max = 1024, message = "业务描述不可以大于{max}")
    private String fieldBizDesc;

    @Override
    public String getCompoundType() {
        return null;
    }

    @Override
    protected Boolean getFillZero() {
        return null;
    }

    @Override
    protected Boolean getUnSigned() {
        return null;
    }

    @Override
    protected Boolean getAutoIncrement() {
        return null;
    }

    @Override
    protected String getInitValue() {
        return null;
    }

    /**
     * 来自数据库的模型
     *
     * @param dbF 来自数据库的模型
     */
    @Override
    public PgSqlFieldData copyDbProperties0(DapDataModelTableField dbF) {
        super.copyDbProperties0(dbF);
        setTableName(dbF.getTableName());
        setFieldName(dbF.getFieldName());
        setFiledDataType(dbF.getDataType());
        setFieldLength(dbF.getLength());
        setDecimalLength(getStrOrNull(dbF.getDecimalLength()));
        setNotNull(Optional.ofNullable(dbF.getNotNull()).orElse(0) == 1);
        setDefaultVal(dbF.getDefaultValue());
        setFieldNameZh(dbF.getComment());
        setFieldBizDesc(dbF.getBusinessDesc());
        setFieldRank(dbF.getFieldRank());
        setPrimaryKey(Optional.ofNullable(dbF.getPrimaryKey()).orElse(0) == 1);
        return this;
    }

    public static PgSqlFieldData getInstance(){
        return new PgSqlFieldData();
    }

}
